<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <link href="../../libs/mui.min.css" rel="stylesheet"/>
    <link href="../../libs/app.css" rel="stylesheet"/>
    <link href="../../css/main.css" rel="stylesheet"/>
    <link href="../../css/search-view.css" rel="stylesheet"/>
    <script src="../../libs/mui.min.js"></script>
    <script src="../../js/jquery-3.1.1.min.js"></script>
    <script src="../../js/url.js"></script>
</head>

<body>

<div class="head">
    <img class="mui-action-back" src="../../images/all-left.png" alt="">
    <p>搜索</p>
</div>

<div class="search-wrapper">
    <input type="text" id="search" placeholder="请输入关键词" maxlength="15">
    <img class="searchBtn" src="../../images/search.png" alt="">
</div>

<div class="content mui-scroll-wrapper">
    <div class="mui-scroll"></div>
</div>

<script>
    var vip, p = 0,
        ERROR_OK = 'S002';
    mui('.mui-scroll-wrapper').scroll({deceleration: 0.0010});

    $(function () {
        vip = localStorage.vip;
        searchlist();
        // 申请会员
        $(document).on('tap', '.helper-context-1-b1', function () {
            href('vip-pay.html');
        });
    });

    // 搜索
    function searchlist(type) {
        if (type == 'more') {
            p++;
        }
        vip == 'true' ? $('.mui-scroll').addClass('vip') : $('.mui-scroll').removeClass('vip');

        $('.searchBtn').on('tap', function () {
            var text = $('#search').val();

            $.ajax({
                url: ur + 'rest/productController/list?name=' + text + '&page=' + p,
                type: 'GET',
                dataType: 'json',
                headers: {
                    'X-Token': localStorage.token
                },
                data: ''
            })
                .done(function (res) {
                    if (res.data.total < 10) {
                        $('.addmore').hide(0);
                    } else if (res.data.total > 10) {
                        $('.addmore').show(0);
                    }
                    var n = res.data.list.length;
                    if (res.meta.code == ERROR_OK && n) {
                        var temp = [];
                        if (vip == 'true') {
                            for (var i in res.data.list) {
                                var item = res.data.list[i];
                                var html =
                                    '<div class="helper-context-1">' +
                                    '<img class="div-img" id="' + item.id + '" src="' + ur + item.image + '" alt="" width="92%">' +
                                    '<div class="vipshow">' +
                                    '<p class="helper-context-1-2">会员价¥<span class="price2">' + item.vipPrice + '</span></p>' +
                                    '<button class="helper-context-1-b2 ' + (item.have ? 'off' : '') + '" id=' + item.id + ' data-have="' + item.have + '">' + (item.have ? '已购买' : '加入购物车') + '</button>' +
                                    '</div>' +
                                    '</div>';
                                temp.push(html);
                            }
                        } else {
                            for (var i in res.data.list) {
                                var item = res.data.list[i];
                                var html =
                                    '<div class="helper-context-1">' +
                                    '<img class="div-img" id="' + item.id + '" src="' + ur + item.image + '" alt="" width="92%">' +
                                    '<p class="helper-context-1-1">价格¥<span class="price1">' + item.price + '</span></p>' +
                                    '<p class="helper-context-1-2">会员价¥<span class="price2">' + item.vipPrice + '</span></p>' +
                                    '<div>' +
                                    '<button class="helper-context-1-b1">会员申请</button>' +
                                    '<button class="helper-context-1-b2 ' + (item.have ? 'off' : '') + '" id=' + item.id + ' data-have="' + item.have + '">' + (item.have ? '已购买' : '加入购物车') + '</button>' +
                                    '</div>' +
                                    '</div>';
                                temp.push(html);
                            }
                        }

                        if (type == 'more') {
                            $('.mui-scroll').append(temp.join(''));
                        } else {
                            $('.mui-scroll').html(temp.join(''));
                        }
                    } else {
                        mui.toast('暂无结果');
                    }
                });

        });
    }

    // 加入购物车
    $(document).on('tap', '.helper-context-1-b2', function addcar(event) {
        var _this = event.target;
        if (!$(_this).data('have')) {
            $.ajax({
                url: ur + 'rest/productController/add-order',
                type: 'POST',
                dataType: 'json',
                headers: {
                    "X-Token": localStorage.token
                },
                data: {
                    "orderId": localStorage.carid,
                    "productId": $(this).attr('id'),
                    "size": '1',
                }
            }).done(function (data) {
                if (data.meta.code == ERROR_OK) {
                    // 刷新购物车
                    mui.toast('成功加入购物车');
                } else {
                    /*plus.nativeUI.closeWaiting();*/
                }
            });
        }
    });
</script>

</body>

</html>